package com.ruoyi.CheryView.controller;

import com.ruoyi.CheryEcos.domain.CheryEcos;
import com.ruoyi.CheryEcos.service.IEcosService;
import com.ruoyi.CheryView.domain.BarData;
import com.ruoyi.CheryView.domain.LineData;
import com.ruoyi.CheryView.domain.PieData;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/CheryView/View")
@Api("index-view")
public class CheryViewController extends BaseController {
    @Autowired
    private IEcosService iEcosService;


    @GetMapping("/line-chart-data")
    public AjaxResult line() {
        List<String> category = new ArrayList<>();
        List<Double> rate = new ArrayList<>();
        CheryEcos ecos = new CheryEcos();
        LocalDate today = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String localDate = today.format(df);
        ecos.set日期(localDate);

        ecos.set车间("22-line");
        List<CheryEcos> ecos22 = iEcosService.selectCheryEcosList(ecos);
        category.add("芜湖-22");
        rate.add(AcceptanceRate(ecos22));

        ecos.set车间("31-line");
        List<CheryEcos> ecos31 = iEcosService.selectCheryEcosList(ecos);
        category.add("芜湖-31");
        rate.add(AcceptanceRate(ecos31));

        ecos.set车间("32-line");
        List<CheryEcos> ecos32 = iEcosService.selectCheryEcosList(ecos);
        category.add("芜湖-32");
        rate.add(AcceptanceRate(ecos32));

        ecos.set车间("51-line");
        List<CheryEcos> ecos51 = iEcosService.selectCheryEcosList(ecos);
        category.add("芜湖-51");
        rate.add(AcceptanceRate(ecos51));

        ecos.set车间("qd-line");
        List<CheryEcos> ecos_qd = iEcosService.selectCheryEcosList(ecos);
        category.add("青岛");
        rate.add(AcceptanceRate(ecos_qd));

        ecos.set车间("dl-line");
        List<CheryEcos> ecos_dl = iEcosService.selectCheryEcosList(ecos);
        category.add("大连");
        rate.add(AcceptanceRate(ecos_dl));

        ecos.set车间("e-line");
        List<CheryEcos> ecos_e = iEcosService.selectCheryEcosList(ecos);
        category.add("鄂尔多斯");
        rate.add(AcceptanceRate(ecos_e));

        ecos.set车间("2-line");
        List<CheryEcos> ecos_2 = iEcosService.selectCheryEcosList(ecos);
        category.add("超二");
        rate.add(AcceptanceRate(ecos_2));

        ecos.set车间("fz-line");
        List<CheryEcos> ecos_fz = iEcosService.selectCheryEcosList(ecos);
        category.add("福州");
        rate.add(AcceptanceRate(ecos_fz));

        return AjaxResult.success(new LineData(category, rate));
    }

    @GetMapping("/pie-chart-data")
    public AjaxResult pie() {
        String name;
        String site;
        LocalDate today = LocalDate.now();
        DateTimeFormatter dF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String localDate = today.format(dF);

        CheryEcos ecos = new CheryEcos();
        ecos.set日期(localDate);


        name = "软件刷写";
        site = "软件刷写";
        PieData pieData1 = getPieDate(ecos, name, site);

        name = "车门检测";
        site = "车门检测";
        PieData pieData2 = getPieDate(ecos, name, site);

        name = "胎压检测";
        site = "胎压检测";
        PieData pieData3 = getPieDate(ecos, name, site);

        name = "整车配置";
        site = "整车配置";
        PieData pieData4 = getPieDate(ecos, name, site);

        name = "功能检测";
        site = "功能检测";
        PieData pieData5 = getPieDate(ecos, name, site);

        name = "四轮定位";
        site = "四轮定位";
        PieData pieData6 = getPieDate(ecos, name, site);

        name = "转毂检测";
        site = "转毂检测";
        PieData pieData7 = getPieDate(ecos, name, site);

        name = "终检报告";
        site = "终检报告";
        PieData pieData8 = getPieDate(ecos, name, site);

        List<PieData> list = new ArrayList<>();
        list.add(pieData1);
        list.add(pieData2);
        list.add(pieData3);
        list.add(pieData4);
        list.add(pieData5);
        list.add(pieData6);
        list.add(pieData7);
        list.add(pieData8);
        return AjaxResult.success(list);
    }

    @GetMapping("/bar-chart-data")
    public AjaxResult bar() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter dF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String localDate = today.format(dF);

        CheryEcos ecos = new CheryEcos();
        ecos.set日期(localDate);

        ecos.set车间("22-line");
        List<CheryEcos> cheryEcos1 = iEcosService.selectCheryEcosList(ecos);
        BarData b1 = new BarData("22-line", getBarRate(cheryEcos1));

        ecos.set车间("31-line");
        List<CheryEcos> cheryEcos2 = iEcosService.selectCheryEcosList(ecos);
        BarData b2 = new BarData("31-line", getBarRate(cheryEcos2));

        ecos.set车间("32-line");
        List<CheryEcos> cheryEcos3 = iEcosService.selectCheryEcosList(ecos);
        BarData b3 = new BarData("32-line", getBarRate(cheryEcos3));

        ecos.set车间("51-line");
        List<CheryEcos> cheryEcos4 = iEcosService.selectCheryEcosList(ecos);
        BarData b4 = new BarData("51-line", getBarRate(cheryEcos4));

        ecos.set车间("qd-line");
        List<CheryEcos> cheryEcos5 = iEcosService.selectCheryEcosList(ecos);
        BarData b5 = new BarData("qd-line", getBarRate(cheryEcos5));

        ecos.set车间("dl-line");
        List<CheryEcos> cheryEcos6 = iEcosService.selectCheryEcosList(ecos);
        BarData b6 = new BarData("dl-line", getBarRate(cheryEcos6));

        ecos.set车间("e-line");
        List<CheryEcos> cheryEcos7 = iEcosService.selectCheryEcosList(ecos);
        BarData b7 = new BarData("e-line", getBarRate(cheryEcos7));

        ecos.set车间("2-line");
        List<CheryEcos> cheryEcos8 = iEcosService.selectCheryEcosList(ecos);
        BarData b8 = new BarData("2-line", getBarRate(cheryEcos8));

        ecos.set车间("fz-line");
        List<CheryEcos> cheryEcos9 = iEcosService.selectCheryEcosList(ecos);
        BarData b9 = new BarData("fz-line", getBarRate(cheryEcos9));

        List<BarData> list = new ArrayList<>();
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        list.add(b5);
        list.add(b6);
        list.add(b7);
        list.add(b8);
        list.add(b9);
        return AjaxResult.success(list);
    }


    public Double AcceptanceRate(List<CheryEcos> items) {
        Double r = 1.0;
        for (CheryEcos item : items) {
            if (item.get站点().equals("车门检测")) {
                r *= 1.0;
            } else {
                r *= (double) item.get一次合格数() / item.get总检数();
            }

        }
        BigDecimal bd = new BigDecimal(r);
        r = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return r;
    }

    public PieData getPieDate(CheryEcos ecos, String name, String site) {
        Integer value = 0;
        ecos.set站点(site);
        List<CheryEcos> items = iEcosService.selectCheryEcosList(ecos);
        for (CheryEcos item : items) {
            value += item.get总检数();
        }
        return new PieData(name, value);
    }


    public List<Integer> getBaTotal(List<CheryEcos> items){
        List<Integer> total =new ArrayList<>(Arrays.asList(0,0,0,0,0,0,0,0));
        String[] site={"软件刷写","车门检测","胎压检测","整车配置",
                "功能检测","四轮定位","转毂检测","终检报告"
        };
        for (CheryEcos item:items) {
            try {
                int i = Arrays.asList(site).indexOf(item.get站点());
                if (i != -1) {
                    total.set(i, item.get总检数());
                }
            }catch (NumberFormatException e){

            }
        }
        return total;
    }
    public List<Integer> getBarSome(List<CheryEcos> items){
        List<Integer> some =new ArrayList<>(Arrays.asList(0,0,0,0,0,0,0,0));
        String[] site={"软件刷写","车门检测","胎压检测","整车配置",
                "功能检测","四轮定位","转毂检测","终检报告"
        };
        for (CheryEcos item:items) {
            try {
                int i = Arrays.asList(site).indexOf(item.get站点());
                if (i != -1) {
                    some.set(i, item.get一次合格数());
                }
            }catch (NumberFormatException e){

            }
        }
        return some;
    }

    public List<Double> getBarRate(List<CheryEcos> items){
        List<Double> rate =new ArrayList<>(Arrays.asList(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0));
        String[] site={"软件刷写","车门检测","胎压检测","整车配置",
                "功能检测","四轮定位","转毂检测","终检报告"
        };
        for (CheryEcos item:items) {
            try {
                int i = Arrays.asList(site).indexOf(item.get站点());
                if (i != -1) {
                    Double r = (double) item.get一次合格数()/item.get总检数();
                    BigDecimal bd = new BigDecimal(r);
                    r = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    rate.set(i,r);
                }
            }catch (NumberFormatException e){

            }
        }
        return rate;
    }

}


